PUBLIC GCD
; Procedure to compute the greatest common divisor of two
; doubleword-size integer parameters passed on the stack.
; The GCD is returned in EAX.
; No other register is changed.  Flags are unchanged.
; Author:  R. Detmer    Revised:  10/97

GCD     PROC   NEAR32
        push   ebp         ; establish stack frame
        mov    ebp,esp
        sub    esp,4       ; space for one local doubleword
        push   edx         ; save EDX
        pushf              ; save flags

        mov    eax,[ebp+8] ; get Number1
        mov    [ebp-4],eax ; GCD := Number1
        mov    edx,[ebp+12]; Remainder := Number1
until0: mov    eax,[ebp-4] ; Dividend := GCD
        mov    [ebp-4],edx ; GCD := Remainder
        mov    edx,0       ; extend Dividend to doubleword
        div    DWORD PTR [ebp-4]   ; Remainder in EDX
        cmp    edx, 0      ; remainder = 0?
        jnz    until0      ; repeat if not

        mov    eax,[ebp-4] ; copy GCD to EAX

        popf               ; restore flags
        pop    edx         ; restore EDX
        mov    esp,ebp     ; restore ESP
        pop    ebp         ; restore EBP
        ret    8           ; return, discarding parameters
GCD     ENDP
        END

